--
-- Created by IntelliJ IDEA.
-- User: bluda
-- Date: 2018/8/20
-- Time: 12:27
-- To change this template use File | Settings | File Templates.
--
local Battlefield = import("app.model.battlefield.Battlefield")

local Status_Area = {width=win.width, height=80,x=0,y=win.height-80}
local Text_Area = {width=win.width, height=180,x=0,y=0}
local Battlefield_Area = {width=win.width, height=win.height-Status_Area.height-Text_Area.height,x=0,y=Text_Area.height}
--local Battlefield_Area = {width=win.width * 5, height=win.height * 5,x=0,y=0}


--local general = {
--    hp = function() return 10000 end,
--    maxHp = function() return 10000 end,
--    fightValue = 100000,
--    attackMode = NEAR_ATTACK,
--    attackWay = "dao1",
--    attackValue = math.random(12, 30),
--    attackDistance = math.random(5, 10),
--    attackSpeed = math.random(0.2, 0.8),
--    moveSpeed = math.random(15, 120),
--    defence = 5,
--    name = "将军",
--    classify = "bubing",
--    weight = 1,
--}
--
--local qiangbing = {
--    hp = 800,
--    maxHp = 800,
--    fightValue = 1200,
--    attackMode = NEAR_ATTACK,
--    attackWay = "qiang1",
--    attackValue = math.random(5, 12),
--    attackDistance = math.random(10, 20),
--    attackSpeed = math.random(0.5, 1.2),
--    moveSpeed = math.random(30, 80),
--    defence = 2.6,
--    name = "枪兵",
--    classify = "qiangbing",
--    weight = 1,
--}
--
--local gongjianshou = {
--    hp = 400,
--    maxHp = 400,
--    fightValue = 600,
--    attackMode = FAR_ATTACK,
--    attackWay = "qiang1",
--    attackValue = math.random(3, 8),
--    attackDistance = math.random(300, 480),
--    attackSpeed = math.random(0.5, 1.2),
--    moveSpeed = math.random(40, 70),
--    defence = 0.7,
--    name = "弓箭兵",
--    classify = "gongjianbing",
--    weight = 1,   
--}


local general = {
    hp = function() return 1000 end,
    maxHp = function() return 1000 end,
    fightValue = function() return 100000 end,
    attackMode = function() return  NEAR_ATTACK end,
    attackWay = function() return  "dao1" end,
    attackValue = function() return  math.random(12, 35) end,
    attackDistance = function() return  math.random(1, 1) end,
    attackSpeed = function() return  math.random(0.6, 1.2) end,
    moveSpeed = function() return  math.random(30, 40) end,
    defence = function() return  5 end,
    name = function() return  "将军" end,
    classify = function() return  "bubing" end,
    weight = function() return  1 end,
    resPrefix = function() return  "xianren" end,
    resStand = function() return  "nm-skin_49-01hand_stand" end,
    resAttack = function() return  "nm-skin_49-01hand_attack6" end,
    resMove = function() return  "nm-skin_49-walk_hand6" end,
    resDeath = function() return  "spineboy-death" end,
    type = function() return  0 end,
    staticRes = function() return  "spineboy-idle_0.png" end,
    shape = function() return 0.7 end,
    class = FightUnit
}

local qiangbing = {
    hp = function() return 240 end,
    maxHp = function() return 240 end,
    fightValue = function() return 1200 end,
    attackMode = function() return  NEAR_ATTACK end,
    attackWay = function() return  "qiang1" end,
    attackValue = function() return  math.random(20, 30) end,
    attackDistance = function() return  math.random(1, 1) end,
    attackSpeed = function() return  math.random(0.6, 1.0) end,
    moveSpeed = function() return  math.random(50, 60) end,
    defence = function() return  5.6 end,
    name = function() return  "枪兵" end,
    classify = function() return  "qiangbing" end,
    weight = function() return  1 end,
    resPrefix = function() return  "futou" end,
    resStand = function() return  "nm-skin_47-01sword_stand2" end,
    resAttack = function() return  "nm-skin_47-01gun_attack10" end,
    resMove = function() return  "nm-skin_47-run_sw1" end,
    resDeath = function() return  "spineboy-death" end,
    type = function() return  1 end,
    staticRes = function() return  "spineboy-idle_0.png" end,
    shape = function() return 0.7 end,
    class = FightUnit
}

local gongjianshou = {
    hp = function() return 150 end,
    maxHp = function() return 150 end,
    fightValue = function() return 600 end,
    attackMode = function() return  FAR_ATTACK end,
    attackWay = function() return  "gongjian1" end,
    attackValue = function() return  math.random(15, 30) end,
    attackDistance = function() return  math.random(8, 10) end,
    attackSpeed = function() return  math.random(0.5, 1.2) end,
    moveSpeed = function() return  math.random(60, 80) end,
    defence = function() return  0.7 end,
    name = function() return  "弓箭兵" end,
    classify = function() return  "gongjianbing" end,
    weight = function() return  1 end,
    resPrefix = function() return  "sheshou" end,
    resStand = function() return  "spineboy-idle" end,
    resAttack = function() return  "spineboy-shoot" end,
    resMove = function() return  "spineboy-run" end,
    resDeath = function() return  "spineboy-death" end,
    type = function() return  2 end,
    staticRes = function() return  "spineboy-idle_0.png" end,
    shape = function() return 0.7 end,
    class = FightUnit
}

local qibing = {
    hp = function() return 600 end,
    maxHp = function() return 600 end,
    fightValue = function() return 600 end,
    attackMode = function() return  NEAR_ATTACK end,
    attackWay = function() return  "qibing1" end,
    attackValue = function() return  math.random(15, 30) end,
    attackDistance = function() return  math.random(1, 1) end,
    attackSpeed = function() return  math.random(0.5, 1.2) end,
    moveSpeed = function() return  math.random(80, 100) end,
    sprintDistance = function() return  math.random(80, 100) end,
    defence = function() return  3.7 end,
    name = function() return  "骑兵" end,
    classify = function() return  "qibing" end,
    weight = function() return  1 end,
    resPrefix = function() return  "futou" end,
    resStand = function() return  "nm-skin_47-01sword_stand2" end,
    resAttack = function() return  "nm-skin_47-01gun_attack10" end,
    resMove = function() return  "nm-skin_47-run_sw1" end,
    resDeath = function() return  "spineboy-death" end,
    type = function() return  2 end,
    staticRes = function() return  "spineboy-idle_0.png" end,
    shape = function() return 0.7 end,
    class = Sowar
}

local GameScene = {
    battleScene = nil,
    battlefield = nil,
    statusLayer = nil,
    textLayer = nil,
    choose = nil,
    chooseItem = nil,
    military = -1
}


function GameScene:create(battleScene)
    battleScene:setContentSize({width=win.width,height=win.height})
    local bkSprite = display.newSprite("backgurand.jpg")
    bkSprite:setPosition(win.width/2,win.height/2)
    local rect = bkSprite:getTextureRect()
    bkSprite:setScaleX(win.width/rect.width)
    bkSprite:setScaleY(win.height/rect.height)
    bkSprite:addTo(battleScene)

    local battleLayer = display.newLayer(cc.c4b(0,0,0,0), Battlefield_Area)
    battleLayer:setPosition(Battlefield_Area)
    battleLayer:addTo(battleScene)

    local bf = Battlefield:create("grassland1", Battlefield_Area, {id="left"}, {id="right"})
    bf:initUI(battleLayer)
    

    local statusLayer = display.newLayer(cc.c4b(111,111,111,215), Status_Area)
    statusLayer:addTo(battleScene)
    statusLayer:setPosition(Status_Area)

    local textLayer = display.newLayer(cc.c4b(111,111,111,215), Text_Area)
    textLayer:setPosition(Text_Area)
    textLayer:addTo(battleScene)

    local textLabel = cc.Label:createWithSystemFont("我是文本显示", "宋体", 20)
    textLabel:move(100, 150):addTo(textLayer)

    GameScene.battleScene = battleScene
    GameScene.battleLayer = battleLayer
    GameScene.battlefield = bf
    GameScene.statusLayer = statusLayer
    GameScene.textLayer = textLayer
    GameScene.textLabel = textLabel

    return GameScene
end

local function initFuncUI(layer, name, choose)
    local layerSize = layer:getContentSize()
    local x,y = layer:getPosition()
    local hCenter = 40

    local button1 = ccui.Button:create()
    button1:setTitleText(name)
    button1:setTitleFontSize(32)
    button1:move(50, hCenter)
    button1:addTo(layer)

    local goSprite = display.newSprite("nm-skin_49-walk_hand6_0.png")
    local stopSprite = display.newSprite("nm-skin_49-walk_hand6_0.png")

    local goToggleMenuItem = cc.MenuItemSprite:create(goSprite, goSprite)
    local stopToggleMenuItem = cc.MenuItemSprite:create(stopSprite,stopSprite)
    local toggleMenuItem = cc.MenuItemToggle:create(goToggleMenuItem, stopToggleMenuItem)
    
    local mn = cc.Menu:create(toggleMenuItem)
    mn:move(110, hCenter)
    layer:addChild(mn)

    toggleMenuItem:registerScriptTapHandler(function(menuItemSender)
        logW("choose zhujiang")
        choose(general)
    end)


    local goSprite = display.newSprite("nm-skin_47-run_sw1_1.png")
    local stopSprite = display.newSprite("nm-skin_47-run_sw1_1.png")
    local goToggleMenuItem = cc.MenuItemSprite:create(goSprite, goSprite)
    local stopToggleMenuItem = cc.MenuItemSprite:create(stopSprite,stopSprite)
    local toggleMenuItem = cc.MenuItemToggle:create(goToggleMenuItem, stopToggleMenuItem)

    local mn = cc.Menu:create(toggleMenuItem)
    mn:move(150, hCenter)
    layer:addChild(mn)

    toggleMenuItem:registerScriptTapHandler(function(menuItemSender)
        logW("choose qiangbing")
        choose(qiangbing)
    end)


    local goSprite = display.newSprite("spineboy-idle_0.png")
    local stopSprite = display.newSprite("spineboy-idle_0.png")
    local goToggleMenuItem = cc.MenuItemSprite:create(goSprite, goSprite)
    local stopToggleMenuItem = cc.MenuItemSprite:create(stopSprite,stopSprite)
    local toggleMenuItem = cc.MenuItemToggle:create(goToggleMenuItem, stopToggleMenuItem)

    local mn = cc.Menu:create(toggleMenuItem)
    mn:move(190, hCenter)
    layer:addChild(mn)

    toggleMenuItem:registerScriptTapHandler(function(menuItemSender)
        logW("choose gongjianshou")
        choose(gongjianshou)
    end)

    local goSprite = display.newSprite("nm-skin_47-run_sw1_1.png")
    local stopSprite = display.newSprite("nm-skin_47-run_sw1_1.png")
    local goToggleMenuItem = cc.MenuItemSprite:create(goSprite, goSprite)
    local stopToggleMenuItem = cc.MenuItemSprite:create(stopSprite,stopSprite)
    local toggleMenuItem = cc.MenuItemToggle:create(goToggleMenuItem, stopToggleMenuItem)

    local mn = cc.Menu:create(toggleMenuItem)
    mn:move(220, hCenter)
    layer:addChild(mn)

    toggleMenuItem:registerScriptTapHandler(function(menuItemSender)
        logW("choose qibing")
        choose(qibing)
    end)
    
end

function GameScene:initFunctionAreaUI()
    local layerSize = self.statusLayer:getContentSize()
    local x,y = self.statusLayer:getPosition()
    logW("initFunctionAreaUI", x, y)
    local hCenter = 40

    local goSprite = display.newSprite("start.png")
    local stopSprite = display.newSprite("pause.png")
    local goToggleMenuItem = cc.MenuItemSprite:create(goSprite, goSprite)
    local stopToggleMenuItem = cc.MenuItemSprite:create(stopSprite,stopSprite)
    local toggleMenuItem = cc.MenuItemToggle:create(goToggleMenuItem, stopToggleMenuItem)
    toggleMenuItem:setScale(0.3)

    local mn = cc.Menu:create(toggleMenuItem)
    mn:setPosition(x + layerSize.width / 2, 40)
    self.statusLayer:addChild(mn)

    toggleMenuItem:registerScriptTapHandler(function(menuItemSender)
        if self.battlefield:isStart() then
            self.battlefield:pause()
        else
            self.battlefield:start()
        end
    end)

    local layer1 = display.newLayer(cc.c4b(111,111,111,215),{width=layerSize.width/2-50, height=layerSize.height})
    layer1:addTo(self.statusLayer)
    initFuncUI(layer1, "军队1", function(choose)
        self.choose = choose
        self.military = 1
        self.chooseItem = nil
        self.textLabel:setString("军队1，选择:" .. choose:name())
    end)

    local layer2 = display.newLayer(cc.c4b(111,111,111,215),{width=layerSize.width/2 -50, height=layerSize.height})
    layer2:setPosition(layerSize.width/2 + 50, 0):addTo(self.statusLayer)
    initFuncUI(layer2, "军队2", function(choose)
        self.choose = choose
        self.military = 2
        self.chooseItem = nil
        self.textLabel:setString("军队2，选择:" .. choose:name())
    end)


    local function btnClick(touch, event)
        logW("eventcode : " .. event:getEventCode(), "type = " .. event:getType())
        if event:getEventCode() == 2 then
            local locationInNode = self.battleLayer:convertToNodeSpace(touch:getLocation())
            -- 取消移动场景
            self.focusMove = false
            self.focusBase = nil
            if inNode(self.battleLayer, locationInNode) then
                if self.choose ~= nil and self.military ~= -1 then
                    self.battlefield:makeSquad(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
--                    self.battlefield:makeFightUnit(self.military, self.choose, locationInNode)
                    logW("放置", self.military, self.choose.name, locationInNode.x, locationInNode.y)
                    return true
                end
                
                if self.chooseItem ~= nil then
                    self.chooseItem:moveOrder(locationInNode)
                end
            end
        elseif event:getEventCode() == 1 then
            -- move
--            local locationInNode = self.battleLayer:convertToNodeSpace(touch:getLocation())
--            if inNode(self.battleLayer, locationInNode) and self.chooseItem ~= nil then
--                self.chooseItem:appendMoveTrack(locationInNode)
--            end
            if self.focusMove == true then
                local locationInNode = touch:getLocation()
                local deltaX = self.focusBase.x - locationInNode.x
                local deltaY = self.focusBase.y - locationInNode.y

                self.focusBase = locationInNode
                logW("move : ", deltaX, deltaY)
                self.battlefield:focusPoint(deltaX, deltaY)
            end
        else 
            -- choose
            local locationInNode = self.textLayer:convertToNodeSpace(touch:getLocation());
            if inNode(self.textLayer, locationInNode) then
                logW("取消选择")
                self.textLabel:setString("")
                self.choose = nil
                self.chooseItem = nil
                return true
            end

            locationInNode = self.battleLayer:convertToNodeSpace(touch:getLocation())
            if inNode(self.battleLayer, locationInNode) then
                local unit = self.battlefield:chooseUnit(locationInNode)
                if unit ~= nil then
                    logW("选择一个单元")
                    self.textLabel:setString("选择".. unit.squad.name)
                    self.chooseItem = unit.squad
                    self.chooseItem:standOrder()
                elseif self.choose == nil and self.chooseItem == nil then 
                    logI("chose bf layer to move.")
                    self.focusMove = true
                    self.focusBase = touch:getLocation()
                end
            end
        end
        return true
    end 
    --创建一个单点触屏事件
    local listener = cc.EventListenerTouchOneByOne:create()
    --注册触屏开始事件
    listener:registerScriptHandler(btnClick, cc.Handler.EVENT_TOUCH_BEGAN)
    --注册触屏移动事件
    listener:registerScriptHandler(btnClick, cc.Handler.EVENT_TOUCH_MOVED)
    --注册触屏结束事件
    listener:registerScriptHandler(btnClick, cc.Handler.EVENT_TOUCH_ENDED)
    --获取层的事件派发器
    local eventDispatcher = self.battleScene:getEventDispatcher()
    --事件派发器 注册一个node事件
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.battleScene)


    local function onMouseScroll(keyCode, event)
        logW("onKeyPressed", keyCode:getScrollX(), keyCode:getScrollY())
        self.battlefield:zoomUp(-keyCode:getScrollY())
    end
    local listener = cc.EventListenerMouse:create()
    listener:registerScriptHandler(onMouseScroll, cc.Handler.EVENT_MOUSE_SCROLL)
    cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener
        , self.battleScene)

    local function onKeyPressed(keyCode, event)
        logW("onKeyPressed", keyCode)
    end
    local function onKeyRelease(keyCode, event)
        logW("onKeyRelease", keyCode)
        if keyCode == 142 and self.chooseItem ~= nil then
            self.chooseItem:keepFormation()
        end
    end
    local listener = cc.EventListenerKeyboard:create()
    listener:registerScriptHandler(onKeyPressed, cc.Handler.EVENT_KEYBOARD_PRESSED)
    listener:registerScriptHandler(onKeyRelease, cc.Handler.EVENT_KEYBOARD_RELEASED)
    cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(listener
        , self.battleScene)
end



return GameScene
